package com.wokeMy.view.channe.rongbangfd;

import android.app.Dialog;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.method.MyApp;
import com.wokeMy.view.adpter.RbRecordAdapter;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.model.RbZd;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class RbRecordCreditActivity extends BaseActivity {
    ImageView reccback;
    ListView recclv;
    String user_id,fdOreJf;
    Dialog dialog;
    ArrayList<RbZd> ghtList ;
    RbRecordAdapter RbRecordAdapter;
    int way_id;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_record_credit);
    }

    @Override
    public void initView() {
        recclv = (ListView) findViewById(R.id.recc_lv);
        reccback = (ImageView) findViewById(R.id.recc_back);
        reccback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void initData() {
        user_id=((MyApp)getApplication()).getDatas_load().getId();
        way_id = getIntent().getIntExtra ("way_id", 0);
        fdOreJf = getIntent().getStringExtra("rb");
        ghtList= new ArrayList<>();
    }
    @Override
    public void initActivity() {
        Log.e("RecordCreditActivity",way_id+"");
        RbRecordAdapter=new RbRecordAdapter(this,ghtList,way_id);
        recclv.setAdapter(RbRecordAdapter);
        dialog= Util.createLoadingDialog(RbRecordCreditActivity.this);
        ghtLog();
    }
    // 荣邦账单记录
    public void  ghtLog(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        String url;
        if (fdOreJf.equals("rbfd")){
            //荣邦封顶
            Log.e("荣邦账单记录url",Constant.RBZD_URL);
            Log.e("荣邦账单记录params",params.toString());
            url=Constant.RBZD_URL;
        }else  {
            //fdOreJf.equals("rbjf") 荣邦积分
            Log.e("荣邦账单记录url",Constant.RBZD_JF_URL);
            Log.e("荣邦账单记录params",params.toString());
            url=Constant.RBZD_JF_URL;
        }

        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(url, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                closeLoadDial(dialog);
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("荣邦账单记录result", result);
                    try {
                        JSONObject jsonObject =new JSONObject(result);
                        if (jsonObject.get("flag").equals("success")) {
                            Gson gson = new Gson();
                            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("data");
                            for (int i1 = 0; i1 < jsonArray.length(); i1++) {
                                RbZd RbZd = gson.fromJson(jsonArray.getString(i1), RbZd.class);
                                ghtList.add(RbZd);
                            }
                            RbRecordAdapter.notifyDataSetChanged();
                        } else {
                            String info = jsonObject.getString("info");
                            if (jsonObject.has("data")){
                                info=jsonObject.getJSONObject("data").getJSONObject("data").getString("dealmsg");
                            }
                            tishi(info);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                closeLoadDial(dialog);
            }
        });
    }

}
